From 88561d305804fe7a5d1a864a6e74854f57f80f2a Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Sat, 20 Oct 2007 09:30:00 +0100 Subject: [PATCH] hvm: Fix PCI-passthru string parsing. Fixes crash of xend during HVM domain ccreation. Signed-off-by: Keir Fraser --- tools/ioemu/hw/pass-through.c | 44 +++++++---------------------- tools/python/xen/lowlevel/xc/xc.c | 46 ++++++++----------------------- 2 files changed, 21 insertions(+), 69 deletions(-) diff --git a/tools/ioemu/hw/pass-through.c b/tools/ioemu/hw/pass-through.c index e4696fb785..b445291874 100644 --- a/tools/ioemu/hw/pass-through.c +++ b/tools/ioemu/hw/pass-through.c @@ -31,48 +31,28 @@ extern FILE *logfile; static int token_value(char *token) { - token = strchr(token, 'x'); - token = token + 1; - - return ((int) strtol(token, NULL, 16)); -} - -static int first_bdf(char *pci_str, char **last, - int *seg, int *bus, int *dev, int *func) -{ - char *token; - - token = strtok_r(pci_str, ",", last); - if ( !token ) - return 0; - - *seg = token_value(token); - token = strtok_r(NULL, ",", last); - *bus = token_value(token); - token = strtok_r(NULL, ",", last); - *dev = token_value(token); - token = strtok_r(NULL, ",", last); - *func = token_value(token); - - return 1; + token = strchr(token, 'x') + 1; + return strtol(token, NULL, 16); } -static int next_bdf(char **last, int *seg, int *bus, int *dev, int *func) +static int next_bdf(char **str, int *seg, int *bus, int *dev, int *func) { char *token; - token = strtok_r(NULL, ",", last); + token = strchr(*str, ','); if ( !token ) return 0; + token++; *seg = token_value(token); - token = strtok_r(NULL, ",", last); + token = strchr(token, ',') + 1; *bus = token_value(token); - token = strtok_r(NULL, ",", last); + token = strchr(token, ',') + 1; *dev = token_value(token); - token = strtok_r(NULL, ",", last); + token = strchr(token, ',') + 1; *func = token_value(token); + *str = token; return 1; } @@ -422,8 +402,6 @@ int pt_init(PCIBus *e_bus, char *direct_pci) int seg, b, d, f; struct pt_dev *pt_dev; struct pci_access *pci_access; - int get_bdf; - char *last = NULL; /* Initialize libpci */ pci_access = pci_alloc(); @@ -436,9 +414,7 @@ int pt_init(PCIBus *e_bus, char *direct_pci) pci_scan_bus(pci_access); /* Assign given devices to guest */ - for ( get_bdf = first_bdf(direct_pci, &last, &seg, &b, &d, &f); - get_bdf; - get_bdf = next_bdf(&last, &seg, &b, &d, &f) ) + while ( next_bdf(&direct_pci, &seg, &b, &d, &f) ) { /* Register real device with the emulated bus */ pt_dev = register_real_device(e_bus, "DIRECT PCI", PT_VIRT_DEVFN_AUTO, diff --git a/tools/python/xen/lowlevel/xc/xc.c b/tools/python/xen/lowlevel/xc/xc.c index 5716dbf2ec..2cf5c5bb73 100644 --- a/tools/python/xen/lowlevel/xc/xc.c +++ b/tools/python/xen/lowlevel/xc/xc.c @@ -531,48 +531,28 @@ static PyObject *pyxc_set_hvm_param(XcObject *self, static int token_value(char *token) { - token = strchr(token, 'x'); - token = token + 1; - - return ((int) strtol(token, NULL, 16)); -} - -static int first_bdf(char *pci_str, char **last, - int *seg, int *bus, int *dev, int *func) -{ - char *token; - - token = strtok_r(pci_str, ",", last); - if ( !token ) - return 0; - - *seg = token_value(token); - token = strtok_r(NULL, ",", last); - *bus = token_value(token); - token = strtok_r(NULL, ",", last); - *dev = token_value(token); - token = strtok_r(NULL, ",", last); - *func = token_value(token); - - return 1; + token = strchr(token, 'x') + 1; + return strtol(token, NULL, 16); } -static int next_bdf(char **last, int *seg, int *bus, int *dev, int *func) +static int next_bdf(char **str, int *seg, int *bus, int *dev, int *func) { char *token; - token = strtok_r(NULL, ",", last); + token = strchr(*str, ','); if ( !token ) return 0; + token++; *seg = token_value(token); - token = strtok_r(NULL, ",", last); + token = strchr(token, ',') + 1; *bus = token_value(token); - token = strtok_r(NULL, ",", last); + token = strchr(token, ',') + 1; *dev = token_value(token); - token = strtok_r(NULL, ",", last); + token = strchr(token, ',') + 1; *func = token_value(token); + *str = token; return 1; } @@ -584,17 +564,13 @@ static PyObject *pyxc_assign_device(XcObject *self, char *pci_str; uint32_t bdf = 0; int seg, bus, dev, func; - int get_bdf; - char *last = NULL; static char *kwd_list[] = { "domid", "pci", NULL }; - if ( !PyArg_ParseTupleAndKeywords(args, kwds, "i|s", kwd_list, + if ( !PyArg_ParseTupleAndKeywords(args, kwds, "is", kwd_list, &dom, &pci_str) ) return NULL; - for ( get_bdf = first_bdf(pci_str, &last, &seg, &bus, &dev, &func); - get_bdf; - get_bdf = next_bdf(&last, &seg, &bus, &dev, &func) ) + while ( next_bdf(&pci_str, &seg, &bus, &dev, &func) ) { bdf |= (bus & 0xff) << 16; bdf |= (dev & 0x1f) << 11; -- 2.30.2